﻿/// <reference path="jquery-1.4.2.min.js" />

var MenuActionsForModelRelatedToVersion = function (emptyPattern, renameAnchor, assignAnchor, unAssignUrl, deleteUrl) {
	this.emptyPattern = emptyPattern;
	this.renameAnchor = renameAnchor;
	this.assignAnchor = assignAnchor;
	this.unAssignUrl = unAssignUrl;
	this.deleteUrl = deleteUrl;
};

MenuActionsForModelRelatedToVersion.prototype.performAction = function (action, elementId) {
	var self = this;
	switch (action) {
		case 'rename':
			var url = self.getTarget(self.renameAnchor, elementId);
			document.location.href = url;
			break;
		case 'assign':
			var url = self.getTarget(self.assignAnchor, elementId);
			document.location.href = url;
			break;
		case 'unAssign':
			var url = self.unAssignUrl.replace(self.emptyPattern, '');
			$.post(url, { id: elementId }, function (data) {
				alert('Solution unassigned');
			});
			break;
		case 'delete':
			var url = self.deleteUrl.replace(self.emptyPattern, '');
			$.ajax({
				async: false,
				type: 'DELETE',
				url: self.deleteUrl,
				data: { id: elementId },
				success: function () {
					document.location.href = document.location.href;
				}
			});
			break;
	}
};

MenuActionsForModelRelatedToVersion.prototype.trimActions = function (element, menu) {
	$(menu).enableContextMenuItems()
	var actionsToDisable = '';
	var classes = $(element).attr('class').toString().split(' ');
	var isAssigned = classes[classes.length - 1] == 'True';
	if (!isAssigned) $(menu).disableContextMenuItems('#unAssign');
	
}

MenuActionsForModelRelatedToVersion.prototype.getTarget = function (anchor, elementId) {
	var self = this;
	return $(anchor).attr('href').toString().replace(self.emptyPattern, elementId);
};

var MenuActionsForIssue = function (emptyPattern, fixAnchor, closeUrl, deleteUrl) {
	this.emptyPattern = emptyPattern;
	this.fixAnchor = fixAnchor;
	this.closeUrl = closeUrl;
	this.deleteUrl = deleteUrl;
};

MenuActionsForIssue.prototype.performAction = function (action, elementId) {
	var self = this;
	switch (action) {
		case 'fix':
			var url = self.getTarget(self.fixAnchor, elementId);
			document.location.href = url;
			break;
		case 'close':
			var url = self.closeUrl.replace(self.emptyPattern, '');
			$.post(url, { id: elementId }, function (data) {
				document.location.href = document.location.href;
			});
			break;
		case 'delete':
			var url = self.deleteUrl.replace(self.emptyPattern, '');
			$.ajax({
				async: false,
				type: 'DELETE',
				url: self.deleteUrl,
				data: { id: elementId },
				success: function () {
					document.location.href = document.location.href;
				}
			});
			break;
	}
};

MenuActionsForIssue.prototype.trimActions = function (element, menu) {
	$(menu).enableContextMenuItems()
	var actionsToDisable = '';
	var classes = $(element).attr('class').toString().split(' ');
	var issueStatus = classes[classes.length -1];
	switch (issueStatus) {
		case 'Open':
			actionsToDisable = '#close';
			break;
		case 'Fixed':
			actionsToDisable = '#fix';
			break;
		case 'Closed':
			actionsToDisable = '#fix,#close';
			break;
	}
	$(menu).disableContextMenuItems(actionsToDisable);
}

MenuActionsForIssue.prototype.getTarget = function (anchor, elementId) {
	var self = this;
	return $(anchor).attr('href').toString().replace(self.emptyPattern, elementId);
};